package com.corechina.apex_osswords.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.corechina.apex_osswords.common.Response;
import com.corechina.apex_osswords.common.SysLog;
import com.corechina.apex_osswords.domain.entity.SysCompany;
import com.corechina.apex_osswords.service.ISysCompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 公司配置表 前端控制器
 * </p>
 *
 * @author cc
 * @since 2022-03-11
 */

@Api(tags = "公司配置表")
@RestController
@RequestMapping("/sys/company")
public class SysCompanyController {

    @Autowired
    private ISysCompanyService iSysCompanyService;

    @ApiOperation("新增")
    @SysLog("新增公司")
    @PostMapping("/add")
    public Response add(@RequestBody SysCompany sysCompany) {
        return iSysCompanyService.add(sysCompany);
    }

    @ApiOperation("编辑")
    @SysLog("编辑公司")
    @PutMapping("/update")
    public Response update(@RequestBody SysCompany sysCompany) {
        return iSysCompanyService.update(sysCompany);
    }

    @ApiOperation("删除")
    @SysLog("删除公司")
    @DeleteMapping("/del")
    public Response del(Integer id) {
        iSysCompanyService.removeById(id);
        return Response.ok();
    }

    @ApiOperation("详情")
    @GetMapping("/getOne")
    public Response getOne(Integer id) {
        return Response.ok(iSysCompanyService.getById(id));
    }

    @ApiOperation("列表")
    @GetMapping("/getList")
    public Response getList(String name, Integer pageIndex, Integer pageSize) {
        QueryWrapper<SysCompany> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.like("name", name);
        }
        queryWrapper.orderByAsc("name");
        Page<SysCompany> page = new Page<>(pageIndex, pageSize);
        iSysCompanyService.page(page, queryWrapper);
        return Response.ok(page.getRecords(), page.getTotal());
    }

}

